import { ResolutionPolicy, UITransform } from 'cc';
import { Camera, Canvas, find, macro, sys, System, View, view } from 'cc';
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('updateCameraSize')
export class updateCameraSize extends Component {

    onLoad(){
        let self = this;

        if (!sys.isNative) {
            window.addEventListener('orientationchange', self.onResize.bind(self), false);
        }

        view.setResizeCallback(self.onResize.bind(self));

        this.onResize();
    }

    // 动态调整相机Size
    onResize() {

        let self = this;

        const designResolution = view.getDesignResolutionSize(); // 获取设计分辨率
        const screenRatio = view.getFrameSize().width / view.getFrameSize().height; // 屏幕宽高比
        const designRatio = designResolution.width / designResolution.height; // 设计宽高比

        console.log(designResolution,screenRatio,designRatio)
      
        if (view.getFrameSize().width > view.getFrameSize().height) {
            console.log('Landspace... 适配高');

            view.setOrientation(macro.ORIENTATION_LANDSCAPE);
            view.setDesignResolutionSize(1334,750,ResolutionPolicy.FIXED_HEIGHT);

            find("Canvas").getComponent(UITransform).width = 1334;
            find("Canvas").getComponent(UITransform).height = 750;

            if((view.getFrameSize().width / view.getFrameSize().height) > 2){
                console.log('L Iphonex... 适配宽');
                view.setDesignResolutionSize(1334,750,ResolutionPolicy.FIXED_WIDTH);
                
            }
        }else{
            view.setOrientation(macro.ORIENTATION_PORTRAIT);
            view.setDesignResolutionSize(750,1334,ResolutionPolicy.FIXED_WIDTH);

            if ((view.getFrameSize().height / view.getFrameSize().width) > 2) {
                console.log('P Iphonex... 适配高');
                view.setDesignResolutionSize(1334,1334,ResolutionPolicy.FIXED_HEIGHT);
            }
        }
        
        find("Canvas").getComponent(UITransform).width = 750;
        find("Canvas").getComponent(UITransform).height = 1334;
    }
}


